"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sleep_1 = __importDefault(require("../sleep"));
const { exec } = require('child_process');
const netWorkMana = {
    cache: null,
    monitoring: false,
    interrupt: false,
    decodeMessage(outStr) {
        const lines = outStr.replaceAll('\r', '').split('\n').filter(item => item);
        if (lines.length < 3) {
            return null;
        }
        else {
            lines.shift();
        }
        let headerList = lines[0].split(',').map((item) => item.split('Network ')[1]).filter((item) => item);
        headerList = headerList.map((item) => {
            return item
                .replace('Interface(', '')
                .replace(')\\Bytes Sent/sec\"', '-发送')
                .replace(')\\Bytes Received/sec\"', '-接收');
        });
        const deviceList = [...new Set(headerList.map((item) => {
                return item.replace('-发送', '').replace('-接收', '');
            }))];
        const dateList = [];
        const speedList = [];
        for (let i = 1; i < lines.length; i++) {
            const lineStr = lines[i];
            const colItem = lineStr.replaceAll('"', '').split(',');
            const dateStr = colItem.shift();
            const speeds = colItem.map(item => Math.floor(Number(item) / 1000));
            if (speeds.length === headerList.length) {
                dateList.push(dateStr);
                speedList.push(speeds);
            }
        }
        return { headerList, dateList, speedList, deviceList };
    },
    async getNetworkList(sc = 20, onMessageFunc) {
        return new Promise((resolve) => {
            const command = `chcp 65001 && typeperf -sc ${sc < 20 ? 20 : sc} "\\Network Interface(*)\\Bytes Sent/sec" "\\Network Interface(*)\\Bytes Received/sec`;
            const child = exec(command);
            let outStr = '';
            child.stdout.on('data', (data) => {
                outStr += data;
                const mes = this.decodeMessage(outStr);
                if (mes) {
                    onMessageFunc && onMessageFunc(this.decodeMessage(outStr));
                }
                if (this.interrupt) {
                    this.interrupt = false;
                    this.monitoring = false;
                    resolve(mes);
                }
            });
            child.stderr.on('data', (data) => {
                console.error(`标准错误: ${data}`);
            });
            child.on('close', (code) => {
                resolve(this.decodeMessage(outStr));
            });
        });
    },
    async getRecently() {
        if (this.cache) {
            return this.cache;
        }
        else {
            this.openListen();
            await (0, sleep_1.default)(3000);
            return this.cache;
        }
    },
    openListen() {
        if (this.monitoring === false) {
            this.monitoring = true;
            this.getNetworkList(360, ((netWork) => {
                if (netWork?.dateList?.length < 50) {
                    this.cache = netWork;
                }
                else {
                    this.cache = {
                        headerList: netWork.headerList,
                        dateList: netWork.dateList.slice(-50),
                        speedList: netWork.speedList.slice(-50),
                        deviceList: netWork.deviceList
                    };
                }
            }));
        }
    },
    stopListen() {
        if (this.monitoring) {
            this.interrupt = false;
        }
        this.monitoring = false;
    }
};
exports.default = netWorkMana;
